package com.zzyl.nursing.service.impl;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author guomo
 * @date 2025-09-29
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        List<NursingProjectPlanVo> nursingPlanVoList = nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingPlanVoList);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto) {
        //添加护理计划
        NursingPlan nursingPlan = new NursingPlan();
//        nursingPlan.setId(dto.getId());
//        nursingPlan.setSortNo(dto.getSortNo());
//        nursingPlan.setPlanName(dto.getPlanName());
//        nursingPlan.setStatus(dto.getStatus());
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(new Date());
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //添加护理项目和护理计划关系
        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        List<NursingProjectPlan> projectPlanList = projectPlans.stream().map(d -> {
            NursingProjectPlan projectPlan = new NursingProjectPlan();
            projectPlan.setPlanId(nursingPlan.getId());   //护理计划的id
            projectPlan.setProjectId(Integer.parseInt(d.getProjectId()));
            projectPlan.setExecuteTime(d.getExecuteTime());
            projectPlan.setCreateTime(new Date());
            projectPlan.setExecuteCycle(d.getExecuteCycle());
            projectPlan.setExecuteFrequency(d.getExecuteFrequency());
            return projectPlan;
        }).collect(Collectors.toList());
        nursingProjectPlanMapper.insertBacth(projectPlanList);
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDto dto) {
        try {
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);
            List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
            if (projectPlans != null && projectPlans.size() > 0) {
                nursingPlanMapper.deleteByPlanId(dto.getId());
                List<NursingProjectPlan> projectPlanList = projectPlans.stream().map(d -> {
                    NursingProjectPlan projectPlan = new NursingProjectPlan();
                    projectPlan.setPlanId(nursingPlan.getId());   //护理计划的id
                    projectPlan.setProjectId(Integer.parseInt(d.getProjectId()));
                    projectPlan.setExecuteTime(d.getExecuteTime());
                    projectPlan.setCreateTime(new Date());
                    projectPlan.setExecuteCycle(d.getExecuteCycle());
                    projectPlan.setExecuteFrequency(d.getExecuteFrequency());
                    return projectPlan;
                }).collect(Collectors.toList());
                nursingProjectPlanMapper.insertBacth(projectPlanList);
            }
            return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException();
        }
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Integer id) {
        try {
            nursingProjectPlanMapper.deleteByPlanId(id);
            return nursingPlanMapper.deleteNursingPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
